package com.oa.cc.action;

import java.util.Date;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.oa.cc.service.AskAuditService;
import com.oa.cc.service.AskService;
import com.oa.cc.service.DeptService;
import com.oa.cc.service.EmpService;
import com.oa.cc.service.MessageService;
import com.oa.cc.service.OaRoleService;
import com.oa.cc.service.WorkStreamStepService;
import com.oa.domain.Ask;
import com.oa.domain.AskAudit;
import com.oa.domain.Dept;
import com.oa.domain.Emp;
import com.oa.domain.Message;
import com.oa.domain.OaRole;
import com.oa.domain.WorkstreamStep;
import com.opensymphony.xwork2.ActionSupport;

public class AskAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private AskService askService;
	private Ask ask;
	private AskAuditService askAuditService;
	private WorkStreamStepService workStreamStepService;
	private DeptService deptService;
	private OaRoleService oaRoleService;
	private EmpService empService;
	private MessageService messageService;
	
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public MessageService getMessageService() {
		return messageService;
	}
	
	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}
	public EmpService getEmpService() {
		return empService;
	}
	
	public void setOaRoleService(OaRoleService oaRoleService) {
		this.oaRoleService = oaRoleService;
	}
	public OaRoleService getOaRoleService() {
		return oaRoleService;
	}
	
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	public DeptService getDeptService() {
		return deptService;
	}
	
	public void setWorkStreamStepService(
			WorkStreamStepService workStreamStepService) {
		this.workStreamStepService = workStreamStepService;
	}
	public WorkStreamStepService getWorkStreamStepService() {
		return workStreamStepService;
	}
	
	public void setAskAuditService(AskAuditService askAuditService) {
		this.askAuditService = askAuditService;
	}
	public AskAuditService getAskAuditService() {
		return askAuditService;
	}
	
	public void setAsk(Ask ask) {
		this.ask = ask;
	}
	public Ask getAsk() {
		return ask;
	}
	
	public void setAskService(AskService askService) {
		this.askService = askService;
	}
	public AskService getAskService() {
		return askService;
	}
	
	//working界面右边操作
	public String doWork() throws Exception {
		//插入申请
		HttpSession session = ServletActionContext.getRequest().getSession();
		Emp emp2 = (Emp) session.getAttribute("emp");
		Emp emp = empService.getEmpsByExample(emp2).get(0);

		System.out.println("登录者部门："+emp.getDept().getDname());
		ask.setEmpno(emp.getEmpno());
		ask.setAtime(new Date());
		ask.setState(0);
		askService.saveAsk(ask);
		
		//插入申请与审核者中间关系表
		ask.setAtime(null);
		ask.setAcontent(null);
		Ask a = askService.getAsksByExample(ask).get(0);
		
		
		WorkstreamStep s = new WorkstreamStep();
		System.out.println("工作流程ID:"+a.getWorkstream().getWsid());
		s.setWorkstream(a.getWorkstream());
		List<WorkstreamStep> list = workStreamStepService.getWorkStreamStepsByExample(s);
		for (int i = 0; i < list.size(); i++) {
			System.out.println("各步骤:" + list.get(i).getStep().getStepname());
		}
		for(WorkstreamStep ws:list){
			AskAudit askAudit = new AskAudit();
			askAudit.setAsk(a);
			askAudit.setState(0);
			System.out.println("所需步骤:"+ws.getStep().getStepname());
			
			Emp e = new Emp();
			Emp auditor = null;
			
			Message message = new Message();
			
			
			//项目组长审核
			if(ws.getStep().getScode()==0){
				e.setDept(emp.getDept());

				OaRole role = oaRoleService.getOaRoleById((long)1005);
				e.setOaRole(role);
				
				auditor = empService.getEmpByDeptAndRole(e);
				
				askAudit.setEmpno(auditor.getEmpno());
				askAuditService.saveAskAudit(askAudit);
			}
			//部门经理审核
			else if(ws.getStep().getScode()==1){
				e.setDept(emp.getDept());

				OaRole role = new OaRole();
				role.setRid((long)1003);
				e.setOaRole(role);
				
				auditor = empService.getEmpByDeptAndRole(e);
				
				askAudit.setEmpno(auditor.getEmpno());
				askAuditService.saveAskAudit(askAudit);
			}
			//行政部审核
			else if(ws.getStep().getScode()==2){
				Dept d = new Dept();
				d.setDeptno(105);
				e.setDept(d);
				OaRole role = new OaRole();
				role.setRid((long)1007);
				e.setOaRole(role);
				
				auditor = empService.getEmpByDeptAndRole(e);
				
				askAudit.setEmpno(auditor.getEmpno());
				askAuditService.saveAskAudit(askAudit);
			}
			//总经理审核
			else if(ws.getStep().getScode()==3){
				
				auditor = empService.getEmpByRoleId((long)1002);
				
				askAudit.setEmpno(auditor.getEmpno());
				askAuditService.saveAskAudit(askAudit);
			}
			//boss审核
			else if(ws.getStep().getScode()==4){
				auditor = empService.getEmpByRoleId((long)1001);
				
				askAudit.setEmpno(auditor.getEmpno());
				askAuditService.saveAskAudit(askAudit);
			}
			message.setEmp(auditor);
			message.setMcontent("你有新的发文要审核");
			message.setMtime(new Date());
			
			messageService.saveMessage(message);
		}
		
		return "workSuccess";
	}
}
